package xyz.weizhi.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.domain.CODE;
import xyz.weizhi.domain.book;
import xyz.weizhi.domain.result;
import xyz.weizhi.service.bookService;

import java.util.List;

/**
 * @Author milian
 * @Date 2021/9/15 0015 9:45
 */

@RestController()
@RequestMapping("/book")
public class bookController {
    @Autowired
    private bookService bookService;
    @GetMapping
    public result selectAllBook() {
        List<book> books = bookService.selectAllBook();
        int i=1/0;
        String msg = books != null ? "" : "数据查询失败，请重试！";
        result result = new result(CODE.SELECT_SUCCESS,books,msg);
        return result;
    }
    @GetMapping("/name")
    public result selectBookByName(@PathVariable String name) {
        List<book> books = bookService.selectBookByName(name);
        Integer code = books != null ? CODE.SELECT_SUCCESS : CODE.SELECT_ERR;
        String msg = books != null ? "" : "数据查询失败，请重试！";
        return new result(code,books,msg);
    }
    @PostMapping
    public result  addBook(@RequestBody book book) {
        Boolean flag = bookService.addBook(book);
        return new result(flag ? CODE.Add_SUCCESS: CODE.Add_ERR,flag);
    }
    @PutMapping("/id")
    public result updateBookById(@PathVariable Integer id) {
        Boolean flag = bookService.updateBookById(id);
        return new result(flag ? CODE.UPDATE_SUCCESS: CODE.UPDATE_ERR,flag);
    }
    @DeleteMapping("/id")
    public result deleteBookById(@PathVariable Integer id) {
        Boolean flag = bookService.deleteBookById(id);
        return new result(flag ? CODE.DELETE_SUCCESS: CODE.DELETE_ERR,flag);
    }
}
